Imports System.Data.SqlClient

Public Class cls_clientevehiculo_

    Implements ICloneable

#Region "Variables ..."
    Private int_idclientevehiculo As Integer = 0
    Private int_idvehiculo As Integer = 0
    Private int_idcliente As Integer = 0
    Private bit_primario As Boolean = False

    Private obj_vehiculo As cls_vehiculo_
    Private obj_cliente As cls_cliente_

    Private col_Items As Collections.Generic.List(Of cls_clientevehiculo_) = Nothing
#End Region

#Region "Propiedades ..."
    Property idclientevehiculo() As Integer
        Get
            idclientevehiculo = int_idclientevehiculo
        End Get
        Set(ByVal value As Integer)
            int_idclientevehiculo = value
        End Set
    End Property
    Property idvehiculo() As Integer
        Get
            idvehiculo = int_idvehiculo
        End Get
        Set(ByVal value As Integer)
            int_idvehiculo = value
        End Set
    End Property
    Property idcliente() As Integer
        Get
            idcliente = int_idcliente
        End Get
        Set(ByVal value As Integer)
            int_idcliente = value
        End Set
    End Property
    Property primario() As Boolean
        Get
            primario = bit_primario
        End Get
        Set(ByVal value As Boolean)
            bit_primario = value
        End Set
    End Property

    Property vehiculo() As cls_vehiculo_
        Get
            If IsNothing(obj_vehiculo) Then
                obj_vehiculo = New cls_vehiculo_
                If Not obj_vehiculo.Buscar(idvehiculo) Then
                    obj_vehiculo = Nothing
                End If
            End If
            vehiculo = obj_vehiculo
        End Get
        Set(ByVal value As cls_vehiculo_)
            obj_vehiculo = value
        End Set
    End Property
    Property cliente() As cls_cliente_
        Get
            If IsNothing(obj_cliente) Then
                obj_cliente = New cls_cliente_
                If Not obj_cliente.Buscar(idcliente) Then
                    obj_cliente = Nothing
                End If
            End If
            cliente = obj_cliente
        End Get
        Set(ByVal value As cls_cliente_)
            obj_cliente = value
        End Set
    End Property
    Property Items() As Collections.Generic.List(Of cls_clientevehiculo_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_clientevehiculo_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_clientevehiculo_)
        While obj_Reader.Read()
            Dim Elemento As New cls_clientevehiculo_
            Elemento.int_idclientevehiculo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idclientevehiculo")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idvehiculo"))) = False Then
                Elemento.int_idvehiculo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idvehiculo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idcliente"))) = False Then
                Elemento.int_idcliente = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcliente")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("primario"))) = False Then
                Elemento.bit_primario = (obj_Reader.GetValue(obj_Reader.GetOrdinal("primario")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idclientevehiculo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idclientevehiculo")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idvehiculo"))) = False Then
            int_idvehiculo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idvehiculo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idcliente"))) = False Then
            int_idcliente = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcliente")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("primario"))) = False Then
            bit_primario = (obj_Reader.GetValue(obj_Reader.GetOrdinal("primario")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "clientevehiculo__Nuevo"
                .Parameters.Add("@idvehiculo", SqlDbType.Int).Value = int_idvehiculo
                .Parameters.Add("@idcliente", SqlDbType.Int).Value = int_idcliente
                .Parameters.Add("@primario", SqlDbType.Bit).Value = bit_primario
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de clientevehiculo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "clientevehiculo__Modificar"
                .Parameters.Add("@idclientevehiculo", SqlDbType.Int).Value = int_idclientevehiculo
                .Parameters.Add("@idvehiculo", SqlDbType.Int).Value = int_idvehiculo
                .Parameters.Add("@idcliente", SqlDbType.Int).Value = int_idcliente
                .Parameters.Add("@primario", SqlDbType.Bit).Value = bit_primario
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de clientevehiculo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idclientevehiculo As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "clientevehiculo__Eliminar"
                .Parameters.Add("@idclientevehiculo", SqlDbType.Int).Value = idclientevehiculo
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de clientevehiculo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idclientevehiculo As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "clientevehiculo__Buscar"
                .Parameters.Add("@idclientevehiculo", SqlDbType.Int).Value = idclientevehiculo
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de clientevehiculo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "clientevehiculo__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de clientevehiculo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "clientevehiculo__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de clientevehiculo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function CargarXidcliente(ByVal prm_idcliente As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXidcliente = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "clientevehiculo__CargarXidcliente"
                .Parameters.Add("@idcliente", SqlDbType.Int).Value = prm_idcliente
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarXidcliente = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarXidcliente los registros de clientevehiculo_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_clientevehiculo_Clon As New cls_clientevehiculo_
        obj_clientevehiculo_Clon.int_idclientevehiculo = Me.int_idclientevehiculo
        obj_clientevehiculo_Clon.int_idvehiculo = Me.int_idvehiculo
        obj_clientevehiculo_Clon.int_idcliente = Me.int_idcliente
        obj_clientevehiculo_Clon.bit_primario = Me.bit_primario
        obj_clientevehiculo_Clon.col_Items = Me.col_Items
        Return obj_clientevehiculo_Clon
    End Function

End Class
